package user

import (
	"wx-uniapp-go/internal/domain/errcode"
	"wx-uniapp-go/internal/logic/coupon_logic"
	"wx-uniapp-go/internal/repository/dto"
	"wx-uniapp-go/internal/server/echo"

	"github.com/gin-gonic/gin"
)

// ReceiveCoupon 领取优惠券
func ReceiveCoupon(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	userId := c.GetInt64("user_id") // 从登录态获取用户ID

	req := new(dto.ReceiveCouponReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	// 设置用户ID
	req.UserId = userId

	if err := coupon_logic.NewCouponLogic(ctx).ReceiveCoupon(req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// ListUserCoupons 查询用户优惠券列表
func ListUserCoupons(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	userId := c.GetInt64("user_id") // 从登录态获取用户ID

	req := new(dto.ListUserCouponReq)
	if err := c.ShouldBindQuery(req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	// 设置用户ID
	req.UserId = userId

	resp, err := coupon_logic.NewCouponLogic(ctx).ListUserCoupons(req)
	if err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c, resp)
}
